<template>
  <div style="min-width:1366px;">
    <!-- 搜索栏区域 -->
    <el-card class="SearchCard" body-style="padding:0px">
      <el-row>
        <el-col :span="2">
          <div class="SearchTitle">条件搜索栏</div>
        </el-col>
        <el-col :span="4">
          <el-row>
            <el-col :span="10">
              <div class="SearchLable">搜索关键字</div>
            </el-col>

            <el-col :span="14">
              <div class="SearchLable">
                <el-input placeholder="请输入内容" v-model="queryInfo.srm" clearable @clear="clearEvent"></el-input>
              </div>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="2">
          <div class="SearchLable">
            <el-button size="medium" type="primary" @click="getSearchList()">搜索</el-button>
          </div>
        </el-col>
        <!-- <el-col :span="2">
          <div class="SearchLable">
            <el-button size="medium" @click="dialogVisible=true" type="primary">添加新药</el-button>
          </div>
        </el-col>-->
      </el-row>
    </el-card>
    <el-card class="TableCard" :body-style="{ padding: '0px' }">
      <div>
        <el-table
          :data="tableData"
          border
          height="550"
          :header-cell-style="{ 'text-align':'center',background: '#3F9DFD', color: '#FFF' }"
          :cell-style="{'text-align':'center'}"
          size="medium"
          stripe
          style="width: 100%;font-size:12px"
        >
          <el-table-column label="序号" type="index"></el-table-column>
          <el-table-column label="规则">
            <template slot-scope="scope">
              <div v-if="scope.row.lactationDrugId>0">
                <el-button
                  type="text"
                  @click="showRuleSuckle(scope.row.lactationDrugId,scope.row.drugName)"
                >查看</el-button>
                <el-button type="text" @click="open(scope.row.drugCode)">删除</el-button>
              </div>

              <el-button
                type="text"
                v-else
                @click="addRuleSuckle(scope.row.drugCode,scope.row.drugName)"
              >新增</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="drugCode" label="药品编码" width="100"></el-table-column>
          <!-- <el-table-column prop="name"
                           label="操作"
                           width="60">
            <el-button type="text"
                       size="mini"
                       @click="dialogVisible = true">编辑</el-button>
          </el-table-column>-->
          <el-table-column prop="drugName" label="药品名称"></el-table-column>

          <!-- <el-table-column prop="cbrand" label="商品名"></el-table-column> -->
          <el-table-column label="药品类型">
            <template slot-scope="scope">
              <div v-if="scope.row.nameType==8">西药</div>
              <div v-else-if="scope.row.nameType==2">草药</div>
              <div v-else-if="scope.row.nameType==6">中成药</div>
            </template>
          </el-table-column>
          <el-table-column prop="drugSpec" label="药品规格"></el-table-column>
          <el-table-column prop="compName" label="药品生产厂家" width="220"></el-table-column>
          <el-table-column prop="approvalCode" label="批准文号"></el-table-column>
          <el-table-column prop="drugSpec" label="批准文号"></el-table-column>
          <el-table-column prop="doseUnit" label="批准文号"></el-table-column>
        </el-table>
      </div>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.page"
          :page-sizes="[20, 50, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="this.total"
          background
        ></el-pagination>
      </div>
    </el-card>
    <el-dialog
      :title="'新增哺乳用药规则'+'—'+addRuleData.drugName"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <div>
        <el-form
          :inline="true"
          :model="addRuleData"
          class="demo-form-inline addForm"
          label-width="150px"
          label-position="right"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="给药类型">
                <el-select v-model="addRuleData.routeType" size="medium" placeholder="请选择">
                  <el-option
                    v-for="item in RouteOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="给药ID">
                <el-input v-model="addRuleData.routeName" size="medium">
                  <el-button slot="append" @click="selectRouteIds(1)">选择</el-button>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="警示级别">
            <el-select v-model="addRuleData.slCode" size="medium" placeholder="请选择">
              <el-option
                v-for="item in SlOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="描述内容">
                <!-- 大框 -->
                <el-input
                  v-model="addRuleData.abstractDesc"
                  type="textarea"
                  :rows="5"
                  placeholder="请输入内容"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="警示信息">
                <!-- 大框 -->
                <el-input
                  v-model="addRuleData.warning"
                  type="textarea"
                  :rows="5"
                  placeholder="请输入内容"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addRuleLactInfo()">新 增</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </span>
    </el-dialog>
    <!-- 查看哺乳用药 -->
    <el-dialog
      :title="'哺乳用药'+'—'+ShowRuleOneData.drugName"
      :visible.sync="ShowSuckRuleVisible"
      width="50%"
      :before-close="handleClose"
    >
      <div>
        <el-form
          :inline="true"
          :model="ShowRuleOneData"
          class="demo-form-inline addForm"
          label-width="150px"
          label-position="right"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="给药类型">
                <el-select v-model="ShowRuleOneData.routeType" size="medium" placeholder="请选择">
                  <el-option
                    v-for="item in RouteOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="给药途径">
                <el-input v-model="ShowRuleOneData.routeName" size="medium">
                  <el-button slot="append" @click="selectRouteIds(2)">选择</el-button>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="警示级别">
                <el-select v-model="ShowRuleOneData.slCode" size="medium" placeholder="请选择">
                  <el-option
                    v-for="item in SlOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12"></el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="描述内容">
                <!-- 大框 -->
                <el-input
                  v-model="ShowRuleOneData.abstractDesc"
                  type="textarea"
                  :rows="5"
                  placeholder="请输入内容"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="警示信息">
                <!-- 大框 -->
                <el-input
                  v-model="ShowRuleOneData.warning"
                  type="textarea"
                  :rows="5"
                  placeholder="请输入内容"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateLactInfo()">更 新</el-button>
        <el-button @click="ShowSuckRuleVisible = false">取消</el-button>
      </span>
    </el-dialog>
    <!-- 给药ID -->
    <el-dialog
      title="选择给药途径"
      :visible.sync="RouteVisible"
      class="editDialog"
      width="30%"
      :before-close="handleClose"
    >
      <div style="width:100%;height:527px">
        <div class="addInput">
          <el-input v-model="RouteData.srm" size="small" placeholder="请输入内容"></el-input>
          <el-button style="margin-right:8px" type="primary" size="small" @click="SearchSuckle()">搜索</el-button>
        </div>
        <el-table
          :data="RouteData.tableData"
          stripe
          border
          height="400"
          style="width: 100%;font-size: 10px"
          :header-cell-style="{'text-align':'center'}"
          :row-style="{height:'40px'}"
          :cell-style="{'text-align':'center',padding:'0px'}"
          @row-click="handleRow"
          @selection-change="handleSelectionChange"
        >
          <el-table-column label width="35">
            <template slot-scope="scope">
              <el-radio
                :label="scope.row"
                v-model="RouteData.radioId"
                @change.native="handleSelectionChange(scope.row)"
              ></el-radio>
            </template>
          </el-table-column>

          <el-table-column prop="routeId" width="150" label="成分编码"></el-table-column>
          <el-table-column prop="routeName" label="给药途径"></el-table-column>
        </el-table>
        <!-- <el-pagination background layout="prev, pager, next" :total="1000"></el-pagination> -->
        <div class="dialogBlock">
          <el-pagination
            @size-change="handleSizeChange1"
            @current-change="handleCurrentChange1"
            current-page.sync="1"
            :page-size="100"
            layout="total, prev, pager, next"
            :total="RouteData.total"
          ></el-pagination>
        </div>
        <span slot="footer" class="dialog-footer">
          <div class="SortFoot">
            <el-button
              style="margin-right:8px"
              type="primary"
              size="small"
              @click="SelectRoute()"
            >确定</el-button>
            <el-button style="margin-right:8px" @click="RouteVisible = false" size="small">取消</el-button>
          </div>
        </span>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getLactationList,
  addRuleLactInfo,
  getLactaRouteList,
  getLactationInfoOne,
  DeleteRuleAdverse,
} from "@/api/rad";
export default {
  data() {
    return {
      multipleSelection: {},
      queryInfo: {
        srm: "",
        pageList: 1,
        pageSize: 20,
      },
      total: 0,
      tableData: [],
      dialogVisible: false,
      addRuleData: {
        drugCode: "",
        drugName: "",
        drugId: "",
        routeType: "",
        routeIds: "",
        routeName: "",
        load: "",
        slCode: "",
        warning: "",
        abstractDesc: "",
      },
      ShowSuckRuleVisible: false,
      ShowRuleOneData: {
        lactationDrugId: "",
        drugName: "",
        drugCode: "",
        drugName: "",
        drugId: "",
        routeType: "",
        routeIds: "",
        routeName: "",
        load: "",
        slCode: "",
        warning: "",
        abstractDesc: "",
      },
      RouteVisible: false,
      RouteData: {
        srm: "",
        page: 1,
        pageSize: 20,
        radioId: [],
        tableData: [],
        total: 0,
      },
      options: [
        {
          value: "1",
          label: "不等于",
        },
        {
          value: "0",
          label: "等于",
        },
      ],
      dateUnit: [
        {
          value: "周",
          label: "周",
        },
        {
          value: "月",
          label: "月",
        },
        {
          value: "年",
          label: "年",
        },
      ],
      RouteOptions: [
        {
          value: 3,
          label: "所有方式给药",
        },
        {
          value: 1,
          label: "全身给药",
        },
        {
          value: 2,
          label: "局部给药",
        },
        {
          value: 0,
          label: "系统默认",
        },
      ],
      SlOptions: [
        {
          value: 0,
          label: "拦截",
        },
        {
          value: 1,
          label: "禁忌",
        },
        {
          value: 2,
          label: "不推荐",
        },
        {
          value: 3,
          label: "轻微",
        },
      ],
    };
  },
  created: function () {
    this.getLactationList();
  },
  methods: {
    handleClose(done) {
      done();
    },
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
    },

    handleCurrentChange(newPage) {
      this.queryInfo.pageList = newPage;
    },
    handleSizeChange1(newSize) {
      this.RouteData.pageSize = newSize;
      this.getSuckleRouteIds();
    },

    handleCurrentChange1(newPage) {
      this.RouteData.page = newPage;
      this.getSuckleRouteIds();
    },
    async getLactationList() {
      let res = await this.$rad.getLactationList({
        srm: this.queryInfo.srm,
        pageList: this.queryInfo.pageList,
        pageSize: this.queryInfo.pageSize,
      });
      (this.tableData = res.rows), (this.total = res.total);
      console.log(res.rows);
    },
    getSearchList() {
      this.getLactationList();
    },
    clearEvent() {},
    addRuleSuckle(drugCode, drugName) {
      this.addRuleData.drugName = drugName;
      this.addRuleData.drugCode = drugCode;
      this.dialogVisible = true;
    },
    selectRouteIds(load) {
      this.addRuleData.load = load;
      this.RouteVisible = true;
      this.getSuckleRouteIds();
    },
    async getSuckleRouteIds() {
      let res = await this.$rad.getLactaRouteList({
        srm: this.RouteData.srm,
        page: this.RouteData.page,
        pageSize: this.RouteData.pageSize,
      });
      console.log(res);
      this.RouteData.tableData = res.rows;
      this.RouteData.total = res.total;
    },
    SearchSuckle() {
      this.getSuckleRouteIds();
    },
    SelectRoute() {
      if (this.addRuleData.load == 1) {
        this.addRuleData.routeIds = this.RouteData.radioId.routeId;
        this.addRuleData.routeName = this.RouteData.radioId.routeName;
      } else if (this.addRuleData.load == 2) {
        this.ShowRuleOneData.routeIds = this.RouteData.radioId.routeId;
        this.ShowRuleOneData.routeName = this.RouteData.radioId.routeName;
      }
      this.RouteVisible = false;
      this.RouteData.srm = "";
    },
    async addRuleLactInfo() {
      let res = await this.$rad.addRuleLactInfo({
        drugCode: this.addRuleData.drugCode,
        // drugId: this.addRuleData.drugId,
        routeType: this.addRuleData.routeType,
        routeIds: this.addRuleData.routeIds,
        slCode: this.addRuleData.slCode,
        warning: this.addRuleData.warning,
        abstractDesc: this.addRuleData.abstractDesc,
      });
      console.log(res);
      if (res.code == 1) {
        this.dialogVisible = false;
        this.getLactationList();
        return this.$message({
          type: "success",
          message: res.msg,
        });
      } else {
        return this.$message({
          type: "error",
          message: res.msg,
        });
      }
    },
    handleRow(row) {
      console.log(row);
      this.RouteData.radioId = row;
      this.multipleSelection = row;
    },
    handleSelectionChange(val) {
      console.log(res);
      this.multipleSelection = val;
    },
    showRuleSuckle(lactationDrugId, drugName) {
      this.ShowRuleOneData.drugName = drugName;
      this.ShowRuleOneData.lactationDrugId = lactationDrugId;
      this.ShowSuckRuleVisible = true;
      this.getLactationInfoOne();
    },
    async getLactationInfoOne() {
      let res = await this.$rad.getLactationInfoOne({
        drugId: this.ShowRuleOneData.lactationDrugId,
      });
      console.log(res);
      this.ShowRuleOneData.drugId = res.data.drugid;
      this.ShowRuleOneData.routeType = res.data.routetype;
      this.ShowRuleOneData.routeIds = res.data.routeids;
      this.ShowRuleOneData.routeName = res.data.routeids;
      this.ShowRuleOneData.slCode = res.data.slcode;
      this.ShowRuleOneData.warning = res.data.warning;
      this.ShowRuleOneData.abstractDesc = res.data.abstracts;
      console.log(this.ShowRuleOneData);
    },
    async updateLactInfo() {
      let res = await this.$rad.updateLactInfo({
        drugId: this.ShowRuleOneData.drugId,
        id: this.ShowRuleOneData.id,
        routeType: this.ShowRuleOneData.routeType,
        routeIds: this.ShowRuleOneData.routeIds,
        slCode: this.ShowRuleOneData.slCode,
        warning: this.ShowRuleOneData.warning,
        abstractDesc: this.ShowRuleOneData.abstractDesc,
      });
      console.log(this.ShowRuleOneData);
      if (res.code == 1) {
        this.ShowSuckRuleVisible = false;
        return this.$message({
          type: "success",
          message: res.msg,
        });
      } else {
        return this.$message({
          type: "error",
          message: res.msg,
        });
      }
    },
    open(drugCode) {
      this.$confirm("此操作将永久删除该规则, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.DeleteRuleAdverse(drugCode);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async DeleteRuleAdverse(drugCode) {
      let res = await this.$rad.DeleteRuleAdverse({
        drugCode: drugCode,
      });
      console.log(res);
      if (res.code == 1) {
        this.getLactationList();
        return this.$message({
          type: "success",
          message: res.msg,
        });
      } else {
        return this.$message({
          type: "error",
          message: res.msg,
        });
      }
    },
  },
};
</script>
<style scoped>
.SearchCard {
  height: 50px;
  font-size: 12px;
}
.SearchTitle {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-right: 1px solid #c2c2c2;
}
.SearchLable {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.TableCard {
  margin-left: 3px;
  margin-top: 2px;
}
.block {
  text-align: center;
  margin-top: 12px;
  margin-bottom: 12px;
}
.editDialog >>> .el-dialog__header {
  margin-bottom: 0px;
}
.editDialog >>> .el-dialog__body {
  padding-left: 0px;
  padding-right: 0px;
}
.addInput {
  height: 45px;
  display: flex;
  align-items: center;
}
.SortFoot {
  height: 50px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.addInput >>> .el-input {
  width: 40%;
  margin-left: 8px;
}
.addInput >>> .el-button {
  margin-left: 8px;
}
.addForm >>> .el-form-item__content {
  width: 200px;
}
</style>